package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.SysRoleMapper;
import com.example.pojo.SysRole;
import com.example.pojo.SysUser;
import com.example.mapper.SysUserMapper;
import com.example.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-02-15
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public Page<UserVo> queryUserVoByCondition(Page<UserVo> page, Map<String, Object> map) {
        QueryWrapper<UserVo> wrapper = new QueryWrapper<>();
        wrapper.apply(" su.del_flag=0 ")
                .eq(!ObjectUtils.isEmpty(map.get("officeId")),"su.office_id",map.get("officeId"))
                .like(!ObjectUtils.isEmpty(map.get("name")),"su.name",map.get("name"));
        baseMapper.queryUserVoByCondition(page,wrapper);
        for (UserVo record : page.getRecords()) {
            List<SysRole> sysRoles = sysRoleMapper.queryByUserId(record.getId());
            record.setSysRoles(sysRoles);
        }
        return page;
    }

    @Override
    public List<SysUser> queryByRoleId(long id) {

        return baseMapper.queryByRoleId(id);
    }

    @Override
    public List<SysUser> queryByQualification(String s) {

        return baseMapper.queryByQualification(s);
    }
}
